<?php

namespace app\controller\admin\yys;

use app\model\api\YysHero;
use app\model\api\YysJx;
use app\model\api\YysSkill;
use app\model\api\YysSource;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Jx
 * @package app\controller\admin\yys
 */
class Jx extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new YysJx();
    }


    //添加
    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        if(empty($post['role_id'])){
            throw new \Exception('请选择角色');
        }

        $source = (new YysSource())->where('type','=','jx')
            ->cache(true)->column('ico','name');

        $insData['role_id']  = $post['role_id'];
        $insData['headimg_jx1']     = $post['headimg_jx1'];
        $insData['headimg_jx2']      = $post['headimg_jx2'];
        $insData['jx_type']     = $post['jx_type'];
        $insData['jx_description']     = $post['jx_description'];




        $insData['material']    = json_encode(array(
            self::transferMaterial($post['material1'],$post['material1_num'],$source),
            self::transferMaterial($post['material2'],$post['material2_num'],$source),
            self::transferMaterial($post['material3'],$post['material3_num'],$source),
            self::transferMaterial($post['material4'],$post['material4_num'],$source)
        ),JSON_UNESCAPED_UNICODE);
        $insData['create_time'] = time();

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        if(empty($post['role_id'])){
            throw new \Exception('请选择角色');
        }

        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        $source = (new YysSource())->where('type','=','jx')
            ->cache(true)->column('ico','name');


        $row->role_id     = $post['role_id'];
        $row->headimg_jx1        = $post['headimg_jx1'];
        $row->headimg_jx2         = $post['headimg_jx2'];
        $row->jx_type        = $post['jx_type'];
        $row->jx_description        = $post['jx_description'];
        $row->material = json_encode(array(
            self::transferMaterial($post['material1'],$post['material1_num'],$source),
            self::transferMaterial($post['material2'],$post['material2_num'],$source),
            self::transferMaterial($post['material3'],$post['material3_num'],$source),
            self::transferMaterial($post['material4'],$post['material4_num'],$source)
        ),JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 材料数据转换
     * @param $material
     * @param $number
     * @param $source
     * @return array
     */
    public function transferMaterial($material,$number,$source){
        if(strpos($material,'-') !== false){
            $data = array(
                'name' => explode('-',$material)[0],
                'ico' => $source[$material],
                'number' => explode('-',$material)[1].'×'.$number,
            );
        }else{
            $data = array(
                'name' => $material,
                'ico' => $source[$material],
                'number' => $number,
            );
        }
        return $data;
    }


    public function material(){
        $material = (new YysSource())->where('type','=','jx')
            ->select();
        return $this->success('数据获取成功', $material);
    }

    public function info(){
        $id = $this->request->param('id');

        $role = $this->model->where('id',$id)->find()->toArray();
        $material   = json_decode($role['material'],true);
        foreach($material as $k => $v){
            if(strpos($v['number'],'×')){
                $number = explode('×',$v['number']);
                $role['material'.($k+1)] = $v['name'].'-'.$number[0];
                $role['material'.($k+1).'_num'] = $number[1];
            }else{
                $role['material'.($k+1)] = $v['name'];
                $role['material'.($k+1).'_num'] = $v['number'];
            }
        }

        return $this->success('获取成功', $role);
    }

}